package com.zufangbao.earth.email.excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import com.demo2do.core.utils.DateUtils;
import com.zufangbao.sun.entity.finance.FinanceReportTitles;
import com.zufangbao.sun.entity.finance.ReportDataGenerator;
import com.zufangbao.sun.entity.finance.ReportTitle;
import com.zufangbao.sun.utils.PropertiesUtil;

@Service("excelFinanceService")
public class ExcelFinanceHandler {

	private static int TABLE_DATA_ROW_BASE_INDEX = 1;
	public String createWorkBook(
			List<ReportDataGenerator> reportDataGenerator,
			BigDecimal totalAmount,FinanceReportTitles[] titles,Date createDate,String companyName) throws IOException{
		Workbook excelWorkBook = create_excel(reportDataGenerator, totalAmount,
				titles);
		return save_excel_to_file(excelWorkBook,createDate, companyName);
	}

	private String save_excel_to_file(Workbook excelWorkBook,Date createDate, String companyName)
			throws FileNotFoundException, IOException {
		String file_path = create_excel_full_path(createDate, companyName);
		FileOutputStream fileOut = new FileOutputStream(file_path);
		excelWorkBook.write(fileOut);
		fileOut.close();
		return file_path;
	}

	private Workbook create_excel(
			List<ReportDataGenerator> reportDataGenerator,
			BigDecimal totalAmount, FinanceReportTitles[] titles) {
		Workbook excelWorkBook = new HSSFWorkbook();
		Sheet sheet = excelWorkBook.createSheet(ExcelHandlerSpec.REPORT_TITLE);
		create_table_title_row(titles, sheet, excelWorkBook);
		int last_row_index = create_table_data_rows(reportDataGenerator, titles,
				excelWorkBook, sheet);
		create_table_summary_row(totalAmount, sheet, last_row_index);
		return excelWorkBook;
	}

	private String create_excel_full_path(Date namesf, String companyName) {
		return PropertiesUtil.getPropertiesValue("mail.properties", "excel.location")+companyName+" 回款足额划拨清单_"
				+ DateUtils.format(namesf, "yyyyMMdd") + ".v1.xls";
	}

	private void create_table_summary_row(BigDecimal totalAmount,
			Sheet todayFinancePaymentReport, int i) {
		Row endRow = todayFinancePaymentReport.createRow((short) i);
		Cell totalCell = endRow.createCell(5);
		totalCell.setCellValue("合计");
		Cell totalcount = endRow.createCell(6);
		totalcount.setCellValue(Double.valueOf(totalAmount.toString()));
	}

	private int create_table_data_rows(
			List<ReportDataGenerator> reportDataGenerator,
			FinanceReportTitles[] titles, Workbook excelWorkBook,
			Sheet sheet) {
		HSSFCellStyle contentStyle = create_excel_content_style(excelWorkBook);
		int data_index = 0;
		for(;data_index<reportDataGenerator.size();data_index++){
			create_table_data_rows(titles, sheet, contentStyle,
					data_index, reportDataGenerator.get(data_index));
		}
		return data_index+ TABLE_DATA_ROW_BASE_INDEX ;
	}

	private void create_table_data_rows(FinanceReportTitles[] titles,
			Sheet sheet, HSSFCellStyle contentStyle, int data_index,
			ReportDataGenerator financePaymentRecord) {
		
		Row contentRow = sheet.createRow((short) data_index+TABLE_DATA_ROW_BASE_INDEX
				);
		Map<String,String> reportData=financePaymentRecord.toReportData();
		for(FinanceReportTitles title_key: titles){
			Cell cell = contentRow.createCell(title_key.ordinal());
			cell.setCellStyle(contentStyle);
			if(title_key.getKey().equals(FinanceReportTitles.NO.getKey())){
				cell.setCellValue(data_index+1);
			}else{
				cell.setCellValue(reportData.get(title_key.getKey()));
			}
			sheet.autoSizeColumn(title_key.ordinal());
		}
	}

	private HSSFCellStyle create_excel_content_style(Workbook excelWorkBook) {
		HSSFFont fontForReportContent = (HSSFFont) excelWorkBook.createFont();
		fontForReportContent.setFontName(ExcelHandlerSpec.FONT_SETTING_FOR_REPORT_CONTENT);
		fontForReportContent.setFontHeightInPoints(ExcelHandlerSpec.FONT_SIZE);
		HSSFCellStyle contentStyle = (HSSFCellStyle) excelWorkBook.createCellStyle();
		contentStyle.setFont(fontForReportContent);
		return contentStyle;
	}

	private void create_table_title_row(FinanceReportTitles[] titles,
			Sheet todayFinancePaymentReport, Workbook excelWorkBook) {
		HSSFCellStyle titleStyle = create_title_style(excelWorkBook);
		Row titleRow = todayFinancePaymentReport.createRow((short) 0);
		for(ReportTitle title_key: titles){
			Cell cell = titleRow.createCell(title_key.ordinal());
			cell.setCellStyle(titleStyle);
			cell.setCellValue(title_key.getKey());
			todayFinancePaymentReport.autoSizeColumn(title_key.ordinal());
		}
	}

	private HSSFCellStyle create_title_style(Workbook excelWorkBook) {
		HSSFFont fontForReportTitle = (HSSFFont) excelWorkBook.createFont();
		fontForReportTitle.setFontName(ExcelHandlerSpec.FONT_SETTING_FOR_REPORT_TITLE);
		fontForReportTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		fontForReportTitle.setFontHeightInPoints(ExcelHandlerSpec.FONT_SIZE);
		HSSFCellStyle titleStyle = (HSSFCellStyle) excelWorkBook.createCellStyle();
		titleStyle.setFont(fontForReportTitle);
		return titleStyle;
	}

}
